package com.cxh.rmsq.ui.adapter.self

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cxh.rmsq.R
import com.cxh.rmsq.common.IntentExtra
import com.cxh.rmsq.db.model.CommunityContent
import com.cxh.rmsq.db.model.JoinRecord
import com.cxh.rmsq.db.model.ResultListBean
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.ui.activity.self.CommentDetailActivity
import com.cxh.rmsq.ui.activity.self.LookUserDetailActivity2
import com.cxh.rmsq.ui.activity.self.PreViewImgOrVideoActivity
import com.cxh.rmsq.ui.activity.self.PublishCommunityActivity
import com.cxh.rmsq.ui.widget.SelectableRoundedImageView
import com.cxh.rmsq.utils.MyGridView
import com.cxh.rmsq.utils.MyUtils
import com.cxh.rmsq.utils.loadImage
import com.cxh.rmsq.utils.loadThumbnail
import com.hjq.shape.layout.ShapeRelativeLayout
import com.hjq.shape.view.ShapeImageView
import com.hjq.shape.view.ShapeTextView
import com.xuexiang.xui.widget.imageview.RadiusImageView
import io.rong.imkit.RongIM
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
 *
 */
class JoinRecordListAdapter(type: Int, private val listener: OnCustomClickListener) :
    BaseQuickAdapter<JoinRecord, BaseViewHolder>(R.layout.item_joinrecord_list),
    LoadMoreModule {
    private val mListener: OnCustomClickListener

    init {
        addChildClickViewIds(R.id.iv_like)
        this.mListener = listener
    }

    var type = type
    private var mData = mutableListOf<String>() //图片

    override fun convert(holder: BaseViewHolder, item: JoinRecord) {
        with(holder) {
            setText(R.id.tv_name, item.huntName)
            setText(
                R.id.tv_progress,
                "打卡进度：" + item.checkInProgressCount + "/" + item.fragmentCount
            )
            setText(
                R.id.tv_time,
                "参与时间：" + TimeUtils.millis2String(item.createTime, "yyyy-MM-dd")
            )
            if (!TextUtils.isEmpty(item.amountEarned)) {
                setText(R.id.tv_money, "+" + item.amountEarned + "元")
            } else {
                setText(R.id.tv_money, "+0元")
            }

            if (1 == type) {//1已完成 0未完成
                getView<TextView>(R.id.tv_progress).isVisible = false
                getView<TextView>(R.id.tv_money).isVisible = true
                getView<ImageView>(R.id.iv_status).isVisible = true
                when (item.reviewStatus) {//reviewStatus 0 带结算  1 通过 2 驳回
                    "0" -> {
                        getView<ImageView>(R.id.iv_status).setImageResource(R.mipmap.icon_join_daijiesuan)
                    }

                    "1" -> {
                        getView<ImageView>(R.id.iv_status).setImageResource(R.mipmap.icon_join_complete)
                    }

                    "2" -> {
                        getView<ImageView>(R.id.iv_status).setImageResource(R.mipmap.icon_join_refuse)
                    }
                }
            } else {
                getView<TextView>(R.id.tv_progress).isVisible = true
                getView<TextView>(R.id.tv_money).isVisible = false
                getView<ImageView>(R.id.iv_status).isVisible = false
            }

            getView<RecyclerView>(R.id.recyclerView).layoutManager = GridLayoutManager(context, 3)
            var listAdapter = ResultListAdapter()
            getView<RecyclerView>(R.id.recyclerView).adapter = listAdapter
            listAdapter.setNewInstance(item.resultList)
            listAdapter.notifyDataSetChanged()

            holder.itemView.setOnClickListener {

            }
        }
    }

    override fun getItemId(position: Int): Long {
        return super.getItemId(position)
    }

    interface OnCustomClickListener {
        fun onItemClick(position: Int, data: JoinRecord?) // 根据需求定义参数
        fun onItemLongClick(position: Int, data: JoinRecord?) // 根据需求定义参数
    }

    class ResultListAdapter() :
        BaseQuickAdapter<ResultListBean, BaseViewHolder>(R.layout.item_join_result_list),
        LoadMoreModule {

        override fun convert(holder: BaseViewHolder, item: ResultListBean) {
            with(holder) {
                setText(R.id.tv_name, "碎片00" + (holder.layoutPosition + 1))
                if (!TextUtils.isEmpty(item.photoUrl)) {
                    getView<TextView>(R.id.tv_location).isVisible = true
                    getView<RadiusImageView>(R.id.riv_img).isVisible = true
                    getView<ImageView>(R.id.iv_img).isVisible = false
                    setText(R.id.tv_location, item.checkInLocation)
                    loadImage(item.photoUrl, getView<RadiusImageView>(R.id.riv_img))
                } else {
                    getView<TextView>(R.id.tv_location).isVisible = false
                    getView<RadiusImageView>(R.id.riv_img).isVisible = false
                    getView<ImageView>(R.id.iv_img).isVisible = true
                }

            }
        }

    }
}